// pages/search/search.js

Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      deptno: options.deptno
    })
    var that = this;
    wx.getStorage({
      key: 'searched' + this.data.deptno,
      success: function (res) {
        var data = res.data;
        console.log(data)
        that.setData({
          histories: data
        })
      },
    })
  },
  ClearClick: function (event){
    console.log("点击了图标！")
    this.setData({
      list: null
    })
  },
  OnClearEvent: function (event) {
    console.log(event)
    wx.removeStorage({
      key: 'searched'+this.data.deptno,
      success: function (res) { },
    });
    this.setData({
      histories: null
    })
  },

  itemOnClick: function (event) {
    console.log(event);
    var that = this;
    var empno = event.currentTarget.dataset.empno;
    var ename = event.currentTarget.dataset.ename;
    var histories = that.data.histories;
    var isExist = false;
    if (histories) {
      for (var index = 0; index < histories.length; index++) {
        var list = histories[index];
        if (list.ename == ename) {
          isExist = true;
          break;
        }
      }
    }
    if (!isExist) {
      if (!histories) {
        histories = []
      }
      histories.push({
        ename: ename,
        empno: empno
      });
      that.setData({
        histories: histories
      });
      wx.setStorage({
        key: 'searched'+this.data.deptno,
        data: histories,
        success: function () {
          console.log("保存成功！")
        }
      })
    }
    wx.navigateTo({
      url: '/pages/info/info?empno=' + empno ,
    })
  },

  searchOnClick: function (event) {
    console.log(event)
    var deptno = this.data.deptno;
    var ename = event.detail.value;
    var that = this;
    if (ename!=""){
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//allSignInfo',
        data: {
          deptno: deptno,
          ename: ename
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          that.setData({
            list: res.data
          })
        }
      })
    }else{
      that.setData({
        list: null
      })
    }
  }
  
})